Skip to content

Fix: URLSanitizer allows SSRF via hostnames without DNS/IP revalidation - #1660

Merged
steam-bell-92 merged 2 commits into
steam-bell-92:mainfrom
Kirtan-pc:fix/URLSanitizer
Jul 12, 2026
Merged

Fix: URLSanitizer allows SSRF via hostnames without DNS/IP revalidation#1660
steam-bell-92 merged 2 commits into
steam-bell-92:mainfrom
Kirtan-pc:fix/URLSanitizer

Conversation

@Kirtan-pc

Copy link
Copy Markdown
Contributor

📝 Description

Summary

Fix SSRF vulnerability where URLSanitizer.validate_url() bypassed blocked-network checks for hostname-based URLs (e.g., http://localhost, http://internal-host.example). The _validate_ip_address() method returned early for non-IP hosts, allowing hostnames that resolve to private IPs to pass validation.

Changes

security/url_sanitizer.py

  • DNS resolution for hostnames: _validate_ip_address() now resolves hostnames via socket.getaddrinfo() and checks all resolved IPs against blocked networks. If resolution fails, an InvalidURLError is raised.
  • Port validation: Added BLOCKED_PORTS set (~80 dangerous ports including SSH:22, MySQL:3306, Redis:6379, Docker:2375, etc.) and allowed_ports constructor parameter for allowlist-based port control. _validate_port() is called during validate_url().
  • _check_blocked_ip: Extracted the blocked-network check into a reusable helper.
  • allowed_ports parameter: Added to both URLSanitizer.__init__ and the convenience validate_url() function.

tests/test_security.py

Added 9 new tests covering:

  • Hostname resolving to private IP (DNS-based SSRF) is blocked
  • allow_localhost / allow_private flags work with hostname-based URLs
  • Unresolvable hostnames raise InvalidURLError
  • Blocked ports (e.g., SSH:22) are rejected
  • Blocked port + private hostname combo is rejected
  • Port allowlist works correctly
  • Non-blocked ports pass validation

🔗 Linked Issue

Closes #1541


📋 Contribution Checklist

  • I have verified that my files are placed in the correct directory.
  • I have tested my changes thoroughly on my local machine.
  • GSSoC 2026: I have been formally assigned to this issue and noted it above.

@vercel

vercel Bot commented Jul 12, 2026

Copy link
Copy Markdown

@Kirtan-pc is attempting to deploy a commit to the Anuj's projects Team on Vercel.

A member of the Team first needs to authorize it.

@steam-bell-92 steam-bell-92 added type:bug Something isn't working level:intermediate gssoc:approved GSSoC 2026 approving tag labels Jul 12, 2026
@steam-bell-92
steam-bell-92 merged commit e345677 into steam-bell-92:main Jul 12, 2026
0 of 4 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🎉 Thank you for your contribution!

Your Pull Request has been merged successfully.
Thanks again for your support! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved GSSoC 2026 approving tag level:intermediate type:bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 [Bug]: URLSanitizer allows SSRF via hostnames without DNS/IP revalidation

2 participants